package kr.co.happenstance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import kr.co.happenstance.adapter.RequestAdapter;
import kr.co.happenstance.lib.MatchingLib;
import kr.co.happenstance.lib.vo.LibResultVo;
import kr.co.happenstance.lib.vo.RequestInfo;
import kr.co.happenstance.lib.vo.RequestListResultVo;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class RequestListActivity extends BaseActivity implements View.OnClickListener, OnItemClickListener{
	protected final String TAG = "<<<RequestListActivity>>>";
	
	private Handler timerHadler;
	private TextView tvCurrentTime;	
	
	/**
	 * ListView Adapter 객체
	 */
	private RequestAdapter requestAdapter; 	
	
	/**
	 * Adapter ArrayList객체
	 */
	private ArrayList<RequestInfo> requestList;	
	
	/**
	 * ListView 객체
	 */
	private ListView lvRequestList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_request_list);
		init(this, C.MENU_ID_REQLIST);
		
		tvCurrentTime = (TextView) this.findViewById(R.id.tv_current_time);
		tvCurrentTime.setText(new SimpleDateFormat("a hh:mm", Locale.ENGLISH).format(new Date()));
		
		requestList = new ArrayList<RequestInfo>();
		requestAdapter = new RequestAdapter(this, requestList);
		
		lvRequestList = (ListView) this.findViewById(R.id.lv_request_list);
		lvRequestList.setAdapter(requestAdapter);
		lvRequestList.setOnItemClickListener(this);
		
		timerHadler = new TimerHandler(this);
		timerHadler.sendEmptyMessage(0);
		
		//setRequestList();
	}	
	
	@Override
	protected void onPause(){
		super.onPause();
		if(timerHadler != null){
			timerHadler.removeMessages(0);
		}	
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		if(timerHadler != null){
			timerHadler.sendEmptyMessage(0);
		}
		setRequestList();
	}	
	
	@Override
	public void onClick(View view) {
		switch(view.getId()){
			case R.id.btn_reload:
				Log.d(TAG, "btn_reload onClick()................");
				setRequestList();
				break;	
		}
	}	
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id){
		Log.d(TAG, "onItemClick postion["+position+"]");
		
		Intent intent = new Intent(this, PushMeetActivity.class);
		intent.putExtra("matchingRequestSeq", requestList.get(position).matchingRequestSeq);
		startActivity(intent);
	}		
	
	
	/**
	 * 요청 리스트 셋팅 메소드
	 */
	private void setRequestList(){
		new RequestListTask().execute();
	}	
	
	/**
	 * 요청 리스트 불러오는 Task 클래스
	 * 
	 * @author Administrator
	 *
	 */
	private class RequestListTask extends AsyncTask<Void, Void, LibResultVo> {
		@Override
		protected void onPreExecute() {
			showProgress();
		}	
		
		@Override
		protected LibResultVo doInBackground(Void... arg0) {
			LibResultVo vo = null;
			try{
				
				MatchingLib lib = new MatchingLib();
				vo = lib.requestList(myApp.id, Double.toString(myLocation.getLatitude()), Double.toString(myLocation.getLongitude()));
				
			}catch(Exception e){
				e.printStackTrace();
			}
			return vo;	
		}
		
		@Override
		protected void onPostExecute(LibResultVo resultVo) {
			closeProgress();
			RequestListResultVo vo = (RequestListResultVo) resultVo;
			if(vo != null && vo.getResult().equals(C.API_RESULT_VALUE_SUCCESS)){
				if(vo.getRequestList() != null && vo.getRequestList().size() > 0){
					
					ArrayList<RequestInfo> tmpRequestList = vo.getRequestList();
					requestList.clear();
					for(RequestInfo v: tmpRequestList){
						requestList.add(v);
					}
					requestAdapter.notifyDataSetChanged();
				}				
			}
		}	
		
	}	
	
	/**
	 * 현재시간과 대기시간 출력 Handler 클래스
	 * 
	 * @author Administrator
	 *
	 */
	private static class TimerHandler extends Handler{
		SimpleDateFormat formatter = new SimpleDateFormat("a hh:mm", Locale.ENGLISH);
		RequestListActivity activity;
		
		public TimerHandler(RequestListActivity activity){
			this.activity = activity;
		}
		
		public void handleMessage(Message msg){
			activity.tvCurrentTime.setText(formatter.format(new Date()));	
			this.sendEmptyMessageDelayed(0,1000);
		}		
	}


}
